package com.dx.mes.common.dao.impl;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.core.dao.impl.GenericHibernateDao;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetBoxXfrDao;
import com.dx.mes.common.dao.IRetToolPtDao;
import com.dx.mes.common.dao.IRetToolStDao;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import static com.dx.mes.common.bean.def.GenericDef.*;


@Repository
public class RetToolStHibernateDao extends GenericHibernateDao<Ret_tool_st, String> implements IRetToolStDao {
	
	@Autowired
	private IRetBoxXfrDao retBoxXfrDao;
	@Autowired
	private IRetToolPtDao retToolPtDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	
	public SimpleRtnBeen comFrcRtnCheckPortOnCrr(String box_id){
		//获取卡匣传送信息
		Ret_box_xfr ret_box_xfr = retBoxXfrDao.get(box_id);
		if(ret_box_xfr == null){
			return new SimpleRtnBeen(E_RET_BOX_XFR + E_READ_NOT_FOUND,
					"未找到卡匣[" + box_id + "]的传送信息");
		}
		
		if(RTD_XFR_CMD_FLG_TON.equals(ret_box_xfr.getXfr_cmd_flg())){
			return new SimpleRtnBeen(E_COMMON_RTD_INVALID_XFR_CMD_FLAG,
					"卡匣[" + box_id + "] Invalid xfr_cmd_flg");
		}
		
		//获取设备状态信息
		Ret_tool_st ret_tool_st = this.get(ret_box_xfr.getXfr_pos_id());
		if(ret_tool_st == null){
			return new SimpleRtnBeen(E_BIS_TOOL + E_READ_NOT_FOUND,
					"未找到设备[" + ret_box_xfr.getXfr_pos_id() + "]状态信息[RET_TOOL_ST]");
		}
		
		//获取设备端口信息
		Ret_tool_ptId tool_ptId = new Ret_tool_ptId();
		tool_ptId.setTool_id_fk(ret_box_xfr.getXfr_pos_id());
		tool_ptId.setPort_id(ret_box_xfr.getXfr_port_id());
		Ret_tool_pt ret_tool_pt = retToolPtDao.get(tool_ptId);
		if(ret_tool_pt == null){
			return new SimpleRtnBeen(E_RET_TOOL_PT + E_READ_NOT_FOUND,
					"未找到设备[" + ret_box_xfr.getXfr_pos_id()
					+ "],端口[" + ret_box_xfr.getXfr_port_id()
					+ "]信息[RET_TOOL_PT]");
		}
		
		if(!PORT_STAT_UDRQ.equals(ret_tool_pt.getPort_stat())){
			return new SimpleRtnBeen(E_COMMON_RTD_INVALID_PORT_STATUS,
					"设备[" + ret_box_xfr.getXfr_pos_id()
					+ "],Port[" + ret_box_xfr.getXfr_port_id()
					+ "],Port状态[" + ret_tool_pt.getPort_stat() 
					+ "],must[" + PORT_STAT_UDRQ + "]");
		}
		
		if(!RTD_AGV_ON.equals(ret_tool_pt.getRtd_flg())){
			return new SimpleRtnBeen(E_COMMON_RTD_INVALID_AGV_MODE,
					"设备[" + ret_box_xfr.getXfr_pos_id()
					+ "],Port[" + ret_box_xfr.getXfr_port_id()
					+ "],RTD_FLG[" + ret_tool_pt.getRtd_flg() 
					+ "],must[" + RTD_AGV_ON + "]");
		}
		
		if(!_YES.equals(ret_tool_pt.getPort_active_flg())){
			return new SimpleRtnBeen(E_COMMON_RTD_INVALID_PORT_ACTIVE_FLG,
					"设备[" + ret_box_xfr.getXfr_pos_id()
					+ "],Port[" + ret_box_xfr.getXfr_port_id()
					+ "],PORT_ACTIVE_FLG[" + ret_tool_pt.getPort_active_flg() 
					+ "],must[" + _YES + "]");
		}
		
		//loader端实体卡匣立即下port
		if(LOAD_PORT_TYPE.equals(ret_tool_pt.getPort_typ())){
			Ret_box ret_box = retBoxDao.get(box_id);
			if(ret_box == null){
				return new SimpleRtnBeen(E_RET_BOX + E_READ_NOT_FOUND,
						"未找到卡匣[" + box_id + "]的信息");
			}
			
			if(!PROC_EMPTY.equals(ret_box.getBox_stat())){
				return new SimpleRtnBeen(_NORMAL);
			}
		}
		
		/**
		 * 根据端口类型判断force return flag是否为Y
		 * 在不为Y的情况下，端口变化时间不超过设定的等待时间则继续等待，否则发送下port指令
		 */
		if(LOAD_PORT_TYPE.equals(ret_tool_pt.getPort_typ()) ||
			COMMON_PORT_TYPE.equals(ret_tool_pt.getPort_typ())){
			if(!_YES.equals(ret_tool_st.getLd_force_rtn_flg()) &&
					Util.getDiffMin(ret_tool_pt.getPort_timestamp(), Util.getCurrentTimestamp()) < ret_tool_st.getLd_r_time()){
				return new SimpleRtnBeen(E_COMMON_RTD_TOOL_ON_WAIT_CONTINUE,
						"设备[" + ret_box_xfr.getXfr_pos_id()
						+ "],Port[" + ret_box_xfr.getXfr_port_id()
						+ "],[" + PORT_STAT_UDRQ 
						+ "]时间未超过设定的[" + ret_tool_st.getLd_r_time() + "]min");
			}
		}else if(UNLOAD_PORT_TYPE.equals(ret_tool_pt.getPort_typ())){
			if(!_YES.equals(ret_tool_st.getUl_force_rtn_flg()) &&
					Util.getDiffMin(ret_tool_pt.getPort_timestamp(), Util.getCurrentTimestamp()) < ret_tool_st.getUl_r_time()){
				return new SimpleRtnBeen(E_COMMON_RTD_TOOL_ON_WAIT_CONTINUE,
						"设备[" + ret_box_xfr.getXfr_pos_id()
						+ "],Port[" + ret_box_xfr.getXfr_port_id()
						+ "],[" + PORT_STAT_UDRQ 
						+ "]时间未超过设定的[" + ret_tool_st.getLd_r_time() + "]min");
			}
		}else{
			return new SimpleRtnBeen(E_COMMON_RTD_INVALID_PORT_TYPE,
					"设备[" + ret_box_xfr.getXfr_pos_id()
					+ "],Port[" + ret_box_xfr.getXfr_port_id()
					+ "],Invalid Port Type");
		}
		
		return new SimpleRtnBeen(_NORMAL);
	}

}
